package com.polaris.live.popup

import android.content.Context
import android.text.Editable
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupRemarkBinding
import com.polaris.live.interface_bag.TextWatcherAdapter

/**
 * RemarkPopup      备注半弹窗
 *
 * @author Created by 半仙 on 2023/9/23/023 14:08
 */
class RemarkPopup(
    context: Context,
    private val remark: String,
    private val onRemarkCallBack: ((isEdit: Boolean, remark: String) -> Unit)
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupRemarkBinding

    private var mIsEdit = false

    override fun getImplLayoutId(): Int {
        return R.layout.popup_remark
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupRemarkBinding.bind(popupImplView)
        mBinding.apply {

            edit.addTextChangedListener(object : TextWatcherAdapter() {
                override fun afterTextChanged(s: Editable?) {
                    val textNum = mBinding.edit.text.length
                    mBinding.textNum.text = "$textNum/140"
                }
            })

            if (remark.isNotEmpty()) {
                edit.setText(remark)
                edit.setSelection(remark.length)
                mIsEdit = true
            }

            icBack.setOnSingleClickListener {
                dismiss()
            }

            save.setOnSingleClickListener {
                onRemarkCallBack.invoke(mIsEdit, edit.text.toString())

                dismiss()
            }
        }
    }
}